import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(LoginState state, Dispatch dispatch, ViewService viewService) {
  return Scaffold(
    appBar: PreferredSize(
      preferredSize: Size.fromHeight(50.0),
      child: AppBar(
        automaticallyImplyLeading: false,
        centerTitle: true,
        elevation: 0,
        title: Text('登录'),
      ),
    ),
    body: GestureDetector(
      onTap: () {
        ///
        ///隐藏输入法
        ///
        FocusScope.of(viewService.context).requestFocus(new FocusNode());
      },
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.fromLTRB(15, 50, 15, 10),
        child: ListView(
          children: <Widget>[
            Row(
              children: <Widget>[
                Expanded(
                  child: TextField(
                    controller:
                        TextEditingController.fromValue(TextEditingValue(
                          ///
                          ///数据都是从state 里面取,赋值也放过去,由 state 管理数据
                          ///
                      text: state.data['name'] == null
                          ? 'null'
                          : state.data['name'],
                    )),
                    onChanged: (value) {
                      ///
                      ///dispatch派遣
                      ///派遣到在 action 里面的动作,既然是派遣,就有传值出现
                      ///
                      dispatch(LoginActionCreator.onInsertUsername(value));
                    },
                    decoration: InputDecoration(
                      hintText: '请输入账号',
                    ),
                  ),
                ),
                IconButton(
                  icon: Icon(Icons.clear),
                  onPressed: () {
                    dispatch(LoginActionCreator.onInsertUsername(null));
                  },
                ),
              ],
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: TextField(
                    controller: state.passwordController,
                    obscureText: true, //设置显示密码
                    decoration: InputDecoration(
                      hintText: '请输入密码',
                    ),
                  ),
                ),
                IconButton(
                  icon: Icon(Icons.clear),
                  onPressed: () {
                    dispatch(LoginActionCreator.onClearPassword());
                  },
                ),
              ],
            ),
            RaisedButton(
              color: Colors.blue,
              child: Text(
                '登录',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
              onPressed: () {
                dispatch(LoginActionCreator.onLogin());
              },
            ),
            RaisedButton(
              child: Text('注册'),
              onPressed: () {
                Navigator.of(viewService.context)
                    .pushNamed('register', arguments: null);
              },
            ),
            Text('OpenApi地址：'),
            Text('https://www.apiopen.top/api.html'),
            Text('登录接口地址：'),
            Text('https://api.apiopen.top/developerLogin'),
            Text('测试账号：1'),
            Text('测试账号密码：1'),
            Text(state.data['name'] == null ? 'null' : state.data['name']),
          ],
        ),
      ),
    ),
  );
}
